/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.view;

import com.model.Perpustakaan;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author admin
 */
public class PanelTambahFotoAnggota extends JPanel {

    private JLabel labelkodeAnggota;
    private JTextField textKodeAnggota;
    private JButton tombolSimpan;
    private JButton tombolCariGambar;
    private File file = null;
    private BufferedImage image ;

    public PanelTambahFotoAnggota() {
        inisialisasi();
    }

    public void inisialisasi() {
        file= new File("no picture.png");
        try {
            image=ImageIO.read(file);
            image.getGraphics();

        } catch (IOException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
        setLayout(new BorderLayout());
        JPanel panelTombol = new JPanel();
        panelTombol.setLayout(new FlowLayout());
        
        labelkodeAnggota = new JLabel("Kode");
        panelTombol.add(labelkodeAnggota);
        textKodeAnggota = new JTextField();
        textKodeAnggota.setColumns(10);
        textKodeAnggota.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                textKodeAnggotaAction();
            }
        });
        panelTombol.add(textKodeAnggota);
        
        tombolCariGambar = new JButton("CariGambar");
        panelTombol.add(tombolCariGambar);
        tombolSimpan = new JButton("Simpan");
        tombolSimpan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                tombolSimpanAction();
            }
        });
        panelTombol.add(tombolSimpan);
        
        tombolCariGambar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                tombolCariGambarAction();
            }
        });
        
        add(panelTombol, BorderLayout.SOUTH);

    }

    public void tombolSimpanAction() { 
        File fileBaru = new File(textKodeAnggota.getText()+".PNG");
        
        try {
            FileOutputStream os = new FileOutputStream(fileBaru);
            FileInputStream is= new FileInputStream(file);
            for (int i = 0; i < file.length(); i++) {
                try {
                    os.write(is.read());
                } catch (IOException ex) {
                    Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
                }      
            }
            try {
                os.close();
            } catch (IOException ex) {
                Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
            }
            JOptionPane.showMessageDialog(null, "tersimpan");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    public void tombolCariGambarAction() {
        JFileChooser chooser = new JFileChooser();
        int status = chooser.showOpenDialog(this);
        if (status == JFileChooser.APPROVE_OPTION) {
            file=chooser.getSelectedFile();
            repaint();
            try {
                image=ImageIO.read(file);
            } catch (IOException ex) {
                Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
            }
            image.getGraphics();
        } 
    }
    
public void textKodeAnggotaAction() {
       File fileBaru = new File(textKodeAnggota.getText()+".PNG");
        try {
            image = ImageIO.read(fileBaru);
        } catch (IOException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
        image.getGraphics();
        repaint();
    }
    

    @Override
     public void paintComponent(Graphics g) {
        g.drawImage(image,
                0, 0, 400, 300,
                0, 0, image.getWidth(null), image.getHeight(null),
                null);
        
    }

}
